package a05不公平分发;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import util.RabbitMqUtils;
import util.SleepUtils;

/**
 * @Author forsake
 * @Description
 *
 * @Date 15:17 2022/12/9
 **/
public class Work06 {
    private static final String ACK_QUEUE_NAME = "unfair_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C2 等待接收消息处理时间较长");
        //消息消费的时候如何处理消息
        DeliverCallback deliverCallback = (
                consumerTag, delivery)-> {String message = new String(delivery.getBody());
                SleepUtils.sleep(20);
                System.out.println("接收到消息:" + message);
                /**
                    * 1.消息标记 tag
                    * 2.是否批量应答未应答消息
                */
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };
        // 不公平分发，prefetchCount预取值，一次从消息队列取的数量
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
        //  采用手动应答
        boolean autoAck = false;
        channel.basicConsume(ACK_QUEUE_NAME, autoAck, deliverCallback, (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        });
    }
}